let express = require('express');
let app = express();
let jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens

exports = module.exports = function (req, res, next) {
    let token = req.headers['x-access-token'];
    console.log(token)
    if (token) {
        jwt.verify(token, "likaiji", (err, decode) => {
            if (decode) {  // 正确token
                req.decoded = decode;
                next();
            } else { //  时间失效的时候/ 伪造的token
                return res.json({'isEffective': false, 'message': "token失效，请重新登录", tokenMessage: err});
            }
        });
    } else {
        return res.status(403).send({
            isEffective: false,
            message: 'No token provided.'
        });
    }
}